/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Practica3;

import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author RUDY
 */
public class frmPractica3_2 extends javax.swing.JFrame {

    DefaultTableModel table_create_model=new DefaultTableModel();
    DefaultListModel list_result=new DefaultListModel();
    public Object[] columns_table_create={"Nro","Char","Byte","Short","Integer","Long","Float","Double","String","Identif. Tupla"};    
    List<Atributo> attrs = new ArrayList<Atributo>();
    List<String> property = new ArrayList<String>();
    List<Comparable> v = new ArrayList<Comparable>();
    /**
     * Creates new form frmPractca3_2
     */
    public frmPractica3_2() {
        initComponents();
        this.setLocationRelativeTo(null); 
        table_create_model.setColumnIdentifiers(columns_table_create); 
        table_create.setModel(table_create_model);
        result.setModel(list_result);
        JTextField textField = new JTextField();
        DefaultCellEditor defaultCellEditor=new DefaultCellEditor(textField);
        for (int i = 1; i < 10; i++) {
            table_create.getColumnModel().getColumn(i).setCellEditor(defaultCellEditor);
        }        
        table_create.setRowHeight(28);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        table_create1 = new javax.swing.JTable();
        ok_save1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        count_atributes = new javax.swing.JTextField();
        ok_create = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        table_create = new javax.swing.JTable();
        ok_save = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        result = new javax.swing.JList();

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        table_create1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(table_create1);

        ok_save1.setText("Aceptar");
        ok_save1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ok_save1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(ok_save1)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(ok_save1)
                .addContainerGap())
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        ok_create.setText("Aceptar");
        ok_create.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ok_createActionPerformed(evt);
            }
        });

        jLabel2.setText("Numero de Tuplas :");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(297, 297, 297)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(count_atributes, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ok_create)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(count_atributes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ok_create))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        table_create.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane4.setViewportView(table_create);

        ok_save.setText("Aceptar");
        ok_save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ok_saveActionPerformed(evt);
            }
        });

        jLabel1.setText("(*) Ingrese los datos en la Tabla.");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel5Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(ok_save))
                            .addGroup(jPanel5Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(jLabel1)))
                        .addGap(0, 806, Short.MAX_VALUE))
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ok_save)
                .addContainerGap())
        );

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        result.setBackground(new java.awt.Color(204, 204, 204));
        result.setSelectionForeground(new java.awt.Color(204, 204, 255));
        jScrollPane1.setViewportView(result);

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(0, 0, 0))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ok_createActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ok_createActionPerformed
        // TODO add your handling code here:
        table_create_model.setNumRows(Integer.parseInt(count_atributes.getText()));
        for (int i = 0; i < Integer.parseInt(count_atributes.getText()); i++) {
               table_create_model.setValueAt(i+1, i, 0);
            }       
    }//GEN-LAST:event_ok_createActionPerformed

    private void ok_save1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ok_save1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ok_save1ActionPerformed

    private void ok_saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ok_saveActionPerformed
        // TODO add your handling code here:
        List<Atributo> attrs = new ArrayList<Atributo>();
        attrs.add(new Atributo("character", Character.class));
        attrs.add(new Atributo("byte", Byte.class));
        attrs.add(new Atributo("short", Short.class));
        attrs.add(new Atributo("integer", Integer.class));
        attrs.add(new Atributo("long", Long.class));
        attrs.add(new Atributo("float", Float.class));
        attrs.add(new Atributo("double", Double.class));
        attrs.add(new Atributo("string", String.class));
        Relacion rel = new Relacion(attrs);
       
        List<GestorIOTupla> listas = new ArrayList<GestorIOTupla>();
        List<Tupla> Tupla = new ArrayList<Tupla>();
        
        list_result.clear();
        int rows=table_create.getRowCount();        
        boolean error=false;
        for (int i = 0; i < rows; i++) {  
            List<Comparable> List_Tuplas = new ArrayList<Comparable>();
            try {
                if(!table_create.getValueAt(i, 1).equals(""))//char
                {List_Tuplas.add(new Character(table_create.getValueAt(i, 1).toString().toCharArray()[0]));}else
                {list_result.addElement("Error: row "+(i+1)+" column 2. \n");error=true;}
            } catch (Exception e) {
                list_result.addElement("Error: row "+(i+1)+" column 2. "+e);error=true;
            }            
            try {
                if(!table_create.getValueAt(i, 2).equals(""))//byte
                {List_Tuplas.add(new Byte(table_create.getValueAt(i, 2).toString()));}else
                {list_result.addElement("Error: row "+(i+1)+" column 3. \n");error=true;}
            } catch (Exception e) {
                list_result.addElement("Error: row "+(i+1)+" column 3. \n"+e);error=true;
            }
            try {
                if(!table_create.getValueAt(i, 3).equals(""))//short
                {List_Tuplas.add(new Short(table_create.getValueAt(i, 3).toString()));}else
                {list_result.addElement("Error: row "+(i+1)+" column 4. \n");error=true;}
            } catch (Exception e) {
                list_result.addElement("Error: row "+(i+1)+" column 4. \n"+e);error=true;
            }
            try {
                if(!table_create.getValueAt(i, 4).equals(""))//integer
                {List_Tuplas.add(new Integer(table_create.getValueAt(i, 4).toString()));}else
                {list_result.addElement("Error: row "+(i+1)+" column 5. \n");error=true;}
            } catch (Exception e) {
                list_result.addElement("Error: row "+(i+1)+" column 5. \n"+e);error=true;
            }
            try {
                if(!table_create.getValueAt(i, 5).equals(""))//long
                {List_Tuplas.add(new Long(table_create.getValueAt(i, 5).toString()));}else
                {list_result.addElement("Error: row "+(i+1)+" column 6. \n");error=true;}
            } catch (Exception e) {
                list_result.addElement("Error: row "+(i+1)+" column 6. \n"+e);error=true;
            }
            try {
                 if(!table_create.getValueAt(i, 6).equals(""))//float
                {List_Tuplas.add(new Float(table_create.getValueAt(i, 6).toString()));}else
                {list_result.addElement("Error: row "+(i+1)+" column 7. \n");error=true;}
            } catch (Exception e) {
                list_result.addElement("Error: row "+(i+1)+" column 7. \n"+e);error=true;
            }
            try {
                if(!table_create.getValueAt(i, 7).equals(""))//double
                {List_Tuplas.add(new Double(table_create.getValueAt(i, 7).toString()));}else
                {list_result.addElement("Error: row "+(i+1)+" column 8. \n");error=true;}
            } catch (Exception e) {
                list_result.addElement("Error: row "+(i+1)+" column 8. \n"+e);error=true;
            }
            try {
                if(!table_create.getValueAt(i, 8).equals(""))//string
                {List_Tuplas.add(new String(table_create.getValueAt(i, 8).toString()));}else
                {list_result.addElement("Error: row "+(i+1)+" column 9. \n");error=true;}
            } catch (Exception e) {
                list_result.addElement("Error: row "+(i+1)+" column 9. \n"+e);error=true;
            }
            String identificador_tupla="";
            try {
                if(!table_create.getValueAt(i, 9).equals(""))//identificador
                {identificador_tupla=table_create.getValueAt(i, 9).toString();}else
                {list_result.addElement("Error: row "+(i+1)+" column 10. \n");error=true;}
            } catch (Exception e) {
                list_result.addElement("Error: row "+(i+1)+" column 10. \n"+e);error=true;
            }
            if(!error)
            {
                Tupla t = new Tupla(new IdentificadorTupla(identificador_tupla, i), List_Tuplas);
                GestorIOTupla gestor = new GestorIOTupla(rel, identificador_tupla);

                Tupla.add(t);
                listas.add(gestor);
            }
        }
        if(!error)
        {
            int inicio =0;
            for (int j = 0; j < rows; j++)
            {
                Tupla t = Tupla.get(j);
                list_result.addElement("------------------------------- "+j+" ----------------------------------");
                list_result.addElement("Escribiendo la Tupla..." + t);
                byte[] bytes = new byte[1024];
                GestorIOTupla a  = listas.get(j);
                a.escribirTupla(t, bytes, inicio);

                list_result.addElement("Mostrando el arreglo de bytes: "+bytes);

                list_result.addElement("Leendo la Tupla...");            
                Par<Tupla, Integer> st = a.leerTupla(bytes, inicio);
                list_result.addElement(st);
                inicio+=st.segundo;
                list_result.addElement("---------------------------------------------------------------------");
                list_result.addElement(" ");
            }
        }
        else
        {
            list_result.addElement("Error.");
        }
    }//GEN-LAST:event_ok_saveActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmPractica3_2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmPractica3_2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmPractica3_2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmPractica3_2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmPractica3_2().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField count_atributes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JButton ok_create;
    private javax.swing.JButton ok_save;
    private javax.swing.JButton ok_save1;
    private javax.swing.JList result;
    private javax.swing.JTable table_create;
    private javax.swing.JTable table_create1;
    // End of variables declaration//GEN-END:variables
}
